import type { NotifyResponse, STREAM_TYPE, TRACK_TYPE, TYPE } from 'nrtc-web'

export const useNRTCHK = createGlobalState(() => {
  const peers = ref<Record<string, NotifyResponse.Peer>>({})

  const producers = ref<Record<string, NotifyResponse.Producer>>({})

  const consumers = ref<Record<string, NotifyResponse.Consumer>>({})

  const cameras = ref<MediaDeviceInfo[]>([])

  const microphones = ref<MediaDeviceInfo[]>([])

  const speakers = ref<MediaDeviceInfo[]>([])

  const camera = ref<MediaDeviceInfo | null>(null)
  const microphone = ref<MediaDeviceInfo | null>(null)
  const speaker = ref<MediaDeviceInfo | null>(null)

  const streams = markRaw<Record<string, MediaStream | null>>({
    audio: null,
    video: null,
    screen: null,
    bgm: null
  })

  const ready = ref<Record<TRACK_TYPE, boolean | undefined>>({
    audio: undefined,
    video: undefined,
    ScreenVideo: undefined,
    ScreenAudio: undefined,
    bgm: undefined
  })

  const publish = ref<Record<TRACK_TYPE, boolean | undefined>>({
    audio: undefined,
    video: undefined,
    ScreenVideo: undefined,
    ScreenAudio: undefined,
    bgm: undefined
  })

  const config = useLocalStorage('rtcConfig', {
    micMute: false,
    micVolume: 100,
    speakMute: false,
    speakVolume: 100
  })

  const volume = ref(0)

  const volumes = ref<Record<string, number>>({})

  return {
    config,
    ready,
    publish,

    volume,
    volumes,

    peers,
    producers,
    consumers,
    camera,
    cameras,
    microphone,
    microphones,
    speaker,
    speakers,
    streams
  }
})
